<template>
  <div class="app-container">
    <div class="filter-container">

      <el-button type="primary" @click="handleAdd">创建抽奖活动</el-button>

    </div>

    <el-table ref="dragTable" v-loading="listLoading" :data="list" row-key="id" fit highlight-current-row
              style="width: 100%;margin-top:30px;"  >
      <el-table-column align="center" label="活动名称"  >
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="活动时间"  >
        <template slot-scope="scope">
          {{ scope.row.begin_time }}至{{ scope.row.end_time }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="参与抽奖次数/抽奖人数"  >
        <template slot-scope="scope">
          {{scope.row.draw_num }}/{{scope.row.draw_user }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="活动状态"  >
        <template slot-scope="scope">
          {{ statusShow[scope.row.status]}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="default" size="mini" @click="handleLog(scope)">
            抽奖记录
          </el-button>
          <el-button type="primary" size="mini" @click="handleEdit(scope)">
            编辑
          </el-button>
          <el-button v-if="scope.row.status==1" type="warning" size="mini" @click="handleStop(scope)">停止</el-button>
          <el-button v-else type="danger" size="mini" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList"/>

  </div>
</template>

<script>
  import waves from '@/directive/waves' // waves directive

  import Pagination from '@/components/Pagination'
  import { deleteBigWheel, getBigWheels, stopBigWheel } from '../../api/activity'


  const statusShow = {
    0:'手动停止' ,1:'正常', 2:'到期停止'
  }

  export default {
    components: { Pagination },
    directives: { waves },

    filters: {
      statusFilter(status) {
        const statusMap = {
          2: 'info',
          1: 'success'
        }
        return statusMap[status]??'danger'
      },
      statusShowFilter(status) {
        return statusShow[status]??'无'
      }
    },
    data() {
      return {
        statusShow,
        daterange:[],
        data: {},
        list: [],
        dialogVisible: false,
        dialogType: 'new',
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          limit:20
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      handleAdd(){
       this.$router.push({name:'activityDetail'})
      },
      daterangeHandle(value)
      {
        this.listQuery.begin_time=value[0]
        this.listQuery.end_time=value[1]
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      async getList() {
        this.listLoading = true
        const res = await getBigWheels(this.listQuery)
        this.list = res.data.data
        this.total = res.data.total
        this.listLoading = false
      },
      handleEdit(scope) {
        this.$router.push({name:'activityDetail',query:{id:scope.row.id}})
      },
      handleLog(scope) {
        this.$router.push({name:'prizeLog',query:{id:scope.row.id}})
      },
      handleStop({ $index, row }) {
        this.$confirm('确定要手动停止吗?', 'Warning', {
          confirmButtonText: '停止',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            await stopBigWheel(row.id)
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.getList()
          })
          .catch(err => {
            console.error(err)
          })
      },
      handleDelete({ $index, row }) {
        this.$confirm('确定要删除吗?', 'Warning', {
          confirmButtonText: '删除',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            await deleteBigWheel(row.id)
            this.list.splice($index, 1)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
          .catch(err => {
            console.error(err)
          })
      },
    }
  }
</script>

<style lang="scss" scoped>
  .app-container {
    .roles-table {
      margin-top: 30px;
    }

    .permission-tree {
      margin-bottom: 30px;
    }
  }
</style>
